package com.example.szh.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.szh.R;
import com.example.szh.adapter.LiveAdapter;
import com.example.szh.adapter.TextBannerAdapter;
import com.example.szh.entity.BaseEntity;
import com.example.szh.entity.LiveEntity;
import com.example.szh.entity.TextBannerEntity;
import com.example.szh.http.HttpCode;
import com.example.szh.mvp.contract.LREContract;
import com.example.szh.mvp.di.DaggerLREConponent;
import com.example.szh.mvp.di.LREMoudes;
import com.example.szh.mvp.presenter.LREPresenter;
import com.example.szh.mvp.view.live.LiveValuesActivity;
import com.example.szh.nextwork.view.BaseFragment;
import com.example.szh.utils.ToastUtils;
import com.example.szh.widget.VideoNewsItemDecoration;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.youth.banner.Banner;
import com.youth.banner.transformer.ZoomOutPageTransformer;

import java.util.HashMap;
import java.util.Map;


public class LiveFragment extends BaseFragment<LREPresenter> implements LREContract.ILREView ,
        OnRefreshLoadMoreListener , BaseQuickAdapter.OnItemClickListener {

    private SmartRefreshLayout livesSmart;
    private RecyclerView liveRv;
    private View liveHead,liveHot,liveTitle;
    private Banner<TextBannerEntity.Values, TextBannerAdapter> live_head_banner;
    private LiveAdapter liveAdapter;
    private TextBannerAdapter textBannerAdapter;

    @Override
    public void refreshAll(BaseEntity entity) {
        if (entity instanceof TextBannerEntity){
            TextBannerEntity textBannerEntity= (TextBannerEntity) entity;
            textBannerAdapter=new TextBannerAdapter(textBannerEntity.getValues());
            live_head_banner.setAdapter(textBannerAdapter)
                    .setOrientation(Banner.VERTICAL)
                    .setPageTransformer(new ZoomOutPageTransformer());
        }else if (entity instanceof LiveEntity){
            LiveEntity liveEntity= (LiveEntity) entity;
            liveAdapter.setNewData(liveEntity.getValues());
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        live_head_banner.start();
    }

    @Override
    public void onStop() {
        super.onStop();
        live_head_banner.stop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        live_head_banner.destroy();
    }

    @Override
    public void refreshRecyckerView(BaseEntity entity) {
        LiveEntity liveEntity= (LiveEntity) entity;
        if (liveEntity.getValues()==null){
            return;
        }
        liveAdapter.setNewData(liveEntity.getValues());

    }

    @Override
    public void loadMoreRecyclerView(BaseEntity entity) {
        LiveEntity liveEntity= (LiveEntity) entity;
        if (liveEntity.getValues()==null){
            ToastUtils.showMsg("最后一页");
            return;
        }
        liveAdapter.addData(liveEntity.getValues());
    }

    @Override
    public int bindLayout() {
        return R.layout.fragment_live;
    }

    @Override
    public void initView() {

        livesSmart = f(R.id.lives_smart);
        liveRv = f(R.id.live_rv);
        liveHead= LayoutInflater.from(getActivity()).inflate(R.layout.view_live_head, null);
        liveHot= LayoutInflater.from(getActivity()).inflate(R.layout.view_live_hot, null);
        liveTitle= LayoutInflater.from(getActivity()).inflate(R.layout.view_live_title, null);
        live_head_banner=liveHead.findViewById(R.id.live_head_banner);
        livesSmart.setOnRefreshLoadMoreListener(this);
        livesSmart.setRefreshFooter(new ClassicsFooter(getActivity()));
        livesSmart.setRefreshHeader(new ClassicsHeader(getActivity()));
        livesSmart.setEnableLoadMore(true);
        livesSmart.setEnableRefresh(true);
        GridLayoutManager manager = new GridLayoutManager(getActivity(), 2);
        manager.setOrientation(GridLayoutManager.VERTICAL);
        liveRv.setLayoutManager(manager);
        liveRv.addItemDecoration(new VideoNewsItemDecoration(getActivity()));
        liveAdapter=new LiveAdapter(R.layout.item_live);
        liveRv.setAdapter(liveAdapter);
        liveAdapter.addHeaderView(liveHead);
        liveAdapter.addHeaderView(liveHot);
        liveAdapter.addHeaderView(liveTitle);
        liveAdapter.setOnItemClickListener(this);
    }

    @Override
    public void inject() {
        DaggerLREConponent.builder().lREMoudes(new LREMoudes(this)).build().injectLivesFragment(this);
    }

    @Override
    public void initData() {
        Map<String, Object> bannerMap = new HashMap<>();
        bannerMap.put("code", HttpCode.TEXTBANNERCODE);
        Map<String,Object> map=new HashMap<>();
        map.put("code", HttpCode.LIVESCODE);
        map.put("user_id", 1);
        p.requestAll(bannerMap,map);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishLoadMore();
        Map<String,Object> map=new HashMap<>();
        map.put("code", HttpCode.LIVESCODE);
        map.put("user_id", 1);
        p.requestLoad(map);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
            refreshLayout.finishRefresh();
            Map<String,Object> map=new HashMap<>();
            map.put("code", HttpCode.LIVESCODE);
            map.put("user_id", 1);
            p.requestRefresh(map);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        LiveEntity.Values item = (LiveEntity.Values) adapter.getItem(position);
        Bundle bundle = new Bundle();
        bundle.putLong("live_id", item.live_id);
        bundle.putString("live_url",item.live_url);
        startPage(bundle, LiveValuesActivity.class);
    }
}